package cn.qiao.threadDemo;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * desc:
 *
 * @author qiao
 * @date 2022年12月07日 22:39
 */
public class ThreadDemo {



    public static void main(String[] args) throws ExecutionException, InterruptedException {


        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                5,
                10,
                3600,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(5),
                new ThreadFactory()
                {
                    private AtomicInteger atomicInteger = new AtomicInteger(0);
                    public Thread newThread(Runnable r) {
                        return new Thread(r, "a" + this.atomicInteger.incrementAndGet());
                    }
                },
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );

        work work = new work();
        work.start();
        System.out.println(Thread.currentThread().getName() + "111");

        Thread thread = new Thread(new work1());
        thread.start();
        System.out.println("==================");
        FutureTask futureTask = new FutureTask(new work2());
        Thread thread1 = new Thread(futureTask);
        thread1.start();
        Object o = futureTask.get();
        System.out.println(o);
    }


    static class work extends Thread {
        @Override
        public void run() {
            Thread thread = Thread.currentThread();
            System.out.println(thread.getName());
            System.out.println("这是一个线程的测试方式");
        }
    }

    static class work1 implements Runnable {

        public void run() {
            System.out.println(Thread.currentThread().getName() + "这是一个实现了Runnable接口线程的测试方法");
        }
    }


    static class work2 implements Callable {

        public Object call() throws Exception {
            System.out.println(Thread.currentThread().getName() + "这是一个实现了Callable接口线程的测试方法");
            return "实现了Callable接口方法的返回值";
        }
    }


}
